package com.shujia.tf;

import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class RealTimeCardWindowProecss  extends ProcessWindowFunction<Long, Tuple3<Long, Long, Long>, Long, TimeWindow> {

    /**
     *
     * @param key key
     * @param context 上下文对象
     * @param elements 窗口内的数据
     * @param out 输出对象
     * @throws Exception
     */
    @Override
    public void process(Long key, Context context, Iterable<Long> elements, Collector<Tuple3<Long, Long, Long>> out) throws Exception {
        //获取窗口结束时间
        TimeWindow window = context.window();
        long endTime = window.getEnd();

        //统计车流量
        Long sum = 0L;
        for (Long element : elements) {
            sum++;
        }

        //将数据发送到下游
        out.collect(Tuple3.of(key, endTime, sum));
    }
}
